﻿using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace blackhouse.mojoPortalExtend {
    public class LinkToPage : HyperLink {

        #region Fields

        private PageSettings cmsPage;
        private SiteSettings siteSettings;
        private string insecureSiteRoot = String.Empty;
        private string secureSiteRoot = String.Empty;
        private bool fPageKey;
        private bool fSystemPage;
        private string name;
        private string title;

        #endregion

        #region Properties

        public string PageKey { get; set; }
        public SystemPage UseSystemPage { get; set; }
        public bool UseCmsData { get; set; }
        public bool RelativeUrl { get; set; }

        #endregion

        #region Constructors

        public LinkToPage() {
            this.UseCmsData = true;
            this.UseSystemPage = (SystemPage)(-1);
            this.RelativeUrl = true;
        }

        #endregion

        #region Page Life Cycle Handlers

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            if (this.SetScenario())
                this.PopulateView();
            else
                this.Visible = false;
        }

        #endregion

        #region Methods

        private bool SetScenario() {

            this.fPageKey = !String.IsNullOrEmpty(this.PageKey);
            this.fSystemPage = Enum.IsDefined(typeof(SystemPage), this.UseSystemPage);

            if (!this.fPageKey && !this.fSystemPage)
                return false;

            this.siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (WebConfigSettings.ResolveFullUrlsForMenuItemProtocolDifferences) {
                secureSiteRoot = WebUtils.GetSecureSiteRoot();
                insecureSiteRoot = WebUtils.GetInSecureSiteRoot();
            }
            else {
                secureSiteRoot = SiteUtils.GetRelativeNavigationSiteRoot();
                insecureSiteRoot = secureSiteRoot;
            }

            if (this.fSystemPage) {
                switch (UseSystemPage) {
                    case SystemPage.SiteMap:
                        this.name = "Mapa Witryny";
                        break;
                    case SystemPage.PrivatePolicy:
                        this.name = "Polityka prywatności";
                        break;
                    default:
                        return false;
                }
                this.title = this.name;
                return true;
            }

            if (this.fPageKey) {
                PageProvider provider = new PageProvider();
                this.cmsPage = provider.FindCmsPageByFunctionality(this.PageKey);
                if (this.cmsPage == null)
                    return false;
                this.name = this.cmsPage.PageName;
                this.title = this.cmsPage.PageTitle;
                if (string.IsNullOrEmpty(this.title))
                    this.title = this.name;
                return mojoPortal.Business.WebHelpers.WebUser.IsInRole(this.cmsPage.AuthorizedRoles);
            }

            return false;
        }

        private void PopulateView() {
            if (this.fSystemPage)
                this.NavigateUrl = this.FormatUrl(this.UseSystemPage);
            if (this.fPageKey)
                this.NavigateUrl = this.FormatUrl(this.cmsPage);
            if (string.IsNullOrEmpty(this.NavigateUrl)) {
                this.Visible = false;
                return;
            }
            if (this.UseCmsData) {
                this.Text = this.name;
                this.ToolTip = this.title;
            }
            if (!this.RelativeUrl)
                this.NavigateUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + this.NavigateUrl;
        }

        private string FormatUrl(SystemPage page) {
            return this.FormatUrl(false, this.GetSystemUrl(page));
        }

        private string GetSystemUrl(SystemPage page) {
            switch (page) {
                case SystemPage.SiteMap:
                    return "~/sitemap.aspx";
                case SystemPage.PrivatePolicy:
                    string ppu = siteSettings.PrivacyPolicyUrl;
                    if (ppu[0] != '~')
                        ppu = "~" + ppu;
                    return ppu;
                default:
                    return "";
            }
        }

        private string FormatUrl(PageSettings page) {
            return FormatUrl(page.RequireSsl, page.Url);
        }

        private string FormatUrl(bool requireSsl, string url) {
            string itemUrl = String.Empty;
            if (SiteUtils.IsSecureRequest()) {
                if ((!requireSsl) && (!siteSettings.UseSslOnAllPages) && (url.StartsWith("~/")))
                    itemUrl = insecureSiteRoot + url.Replace("~/", "/");

                else
                    itemUrl = secureSiteRoot + url.Replace("~/", "/");
            }
            else {
                if ((requireSsl) || (siteSettings.UseSslOnAllPages)) {
                    if (url.StartsWith("~/"))
                        itemUrl = secureSiteRoot + url.Replace("~/", "/");
                }
                else {
                    if (url.StartsWith("~/"))
                        itemUrl = insecureSiteRoot + url.Replace("~/", "/");
                }
            }
            return itemUrl;
        }

        #endregion

    }

    public enum SystemPage {
        SiteMap,
        PrivatePolicy
    }

}
